var app=getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    nums:33,
    commitsArray:[],
    id:'',
    nickName:'',
    imgArray:[],
    time:'',
    commits:'',
    avatar:'',
    title:''
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id=options.id
      // wx.request({
      //   url: '',
      // })
      this.setData({
        id:id
      })
      var that=this;
    wx.request({
      url: app.globalData.baseUrl + 'onlinequestion/question/'+id,

      header: {
        'cookie':'JSESSIONID='+ wx.getStorageSync('sessionID')
      },
      method:'GET',
      success:function(res){
        console.log(res)
        var resData=res.data.data
        try{
            
          that.setData({
            title: resData.content,
            commits: resData.commentCount,
            nums: resData.commentCount,
            time: resData.gmtCreateFormat.substring(0, 10) + '   ' + resData.gmtCreateFormat.substring(11, 16),
            avatar: resData.user.avatarUrl,
            imgArray:resData.imgList,
            nickName: resData.user.nickName
          })
          for (var i = 0; i < resData.commentList.length; i++) {
            var list = resData.commentList
            var avatar = 'commitsArray[' + i + '].avatar'
            var nickName = 'commitsArray[' + i + '].nickName'
            var date = 'commitsArray[' + i + '].date'
            var content = 'commitsArray[' + i + '].content'
            that.setData({
              [avatar]: list[i].user.avatarUrl,
              [nickName]: list[i].user.nickName,
              [date]: list[i].gmtCreateFormat.substring(0, 10) + '  ' + list[i].gmtCreateFormat.substring(11, 13),
              [content]: list[i].content,

            })

          }
        }catch(err){
          console.log(err)
        }
        
      
      }
    })
  },
  enter:function(event){
    var that=this
    var val=event.detail.v
    wx.request({
      url: app.globalData.baseUrl+'admin/integral/integral/1020',
      method:'get',
      header:{
        'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
      },
      success:function(res){
        console.log(res)
        if(that.data.commitsArray.length<3){
          wx.showModal({
            title: '',
            content: '前三条评论将获得'+(res.data.data.numerical)+'积分',
          })
        }
       
      }
    }),
    wx.request({
      url: app.globalData.baseUrl+'onlinequestion/comment/'+that.data.id,
      header:{
        'content-type':'application/x-www-form-urlencoded',
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      data:{
        content:val
      },
      method:'POST',
      success:function(res){
        if(res.data.msg=='请求成功'){
          wx.showToast({
            title: '发布评论成功',
          })
          var o={
            id:that.data.id
          }
          that.onLoad(o);
        }
        else{
          wx.showToast({
            title: '发布评论失败',
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
     
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})